package cn.acewill.yxd.yunxundian.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.model.Schedule;
import cn.acewill.yxd.yunxundian.model.Shop;

/**
 * Created by XXX on 2017/5/8.
 */

public class TaskRecycleAdapter extends RecyclerView.Adapter<TaskRecycleAdapter.MyViewHolder> {
    protected static YXDAPP yxdapp=new YXDAPP();
    protected static DbManager db = x.getDb(yxdapp.getDaoConfig());
    private List<Schedule> list;
    private Activity activity;
    public MyCallBackClickListener mListener;


    public TaskRecycleAdapter(Activity activity, List<Schedule> list,MyCallBackClickListener mListener){
        this.activity=activity;
        this.list=list;
        this.mListener = mListener;
    }
    public interface MyCallBackClickListener{
        void lookClick(Schedule schedule,int positon);
        void submitClick(Schedule tag, int positon);
        void shopSignClick(Schedule tag, int positon);
        void addinfoClick(Schedule tag, int positon);
        void goDetailClick(Schedule tag,int position);
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =LayoutInflater.from(activity).inflate(R.layout.schedule_task_item,parent,false);
        MyViewHolder holder= new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        Schedule bean = list.get(position);
        holder.bindTag(bean);
        if (list != null) {
            holder.TaskTitle.setText(bean.getTitle());
            if (bean.getSchEndTime() != null)
                holder.startTime.setText(bean.getSchStartTime().replace('T', ' '));
            else
                holder.startTime.setText(bean.getSchStartTime().split("T")[0]);
            int shopID = bean.getShopID();
            try {
                if (shopID == 0) {
                    holder.shopName.setText("非门店任务");
                    holder.shopName.setVisibility(View.GONE);
                }
                else if(shopID==-1&&bean.getIsAssign()==-1)
                {
                    if(bean.getSubtitle()=="备注")
                    {
                        holder.shopName.setText("备注");
                    }
                }
                else {
                    Shop shop = db.selector(Shop.class).where("ID", "=", shopID).findFirst();
                    if (shop != null) {
                        String shopname = shop.getShopName();
                        holder.shopName.setText(shopname);
                    }
                    else {
                        holder.shopName.setText("正在取名中..");
                    }
                }
            } catch (DbException e) {
                e.printStackTrace();
            }
        }
        String schtime = bean.getSchStartTime().replace("T"," ");
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm", Locale.getDefault());
        String thisdate = format.format(date);
        int result = compare_date(schtime, thisdate);
        if(bean.getIsAssign()==-1 && bean.getShopID()==-1)//备注假期不需要颜色的判断
        {
            result = 0;
        }


       /*由不同的日程状态判断颜色*/
        if (bean.getIsHidden() == 1) {
            holder.TaskTitle.setTextColor(ContextCompat.getColor(activity, R.color.hintTask));
            holder.shopName.setTextColor(ContextCompat.getColor(activity, R.color.hintTask));
            holder.startTime.setTextColor(ContextCompat.getColor(activity, R.color.hintTask));
        }
        if (bean.getIsAssign() == 1) {
            holder.cardView.setBackgroundColor(ContextCompat.getColor(activity, R.color.omYellow));
        }
        else if (bean.getStartUserId()!=bean.getToUserId()) {
            holder.cardView.setBackgroundColor(ContextCompat.getColor(activity, R.color.omYellow));
        }

        switch (bean.getStates()) {
            case 1:
                    /*日程时间大于当前时间*/
                if (result == -1) {
                    holder.TaskTitle.setTextColor(ContextCompat.getColor(activity, R.color.delayTask));
                    holder.shopName.setTextColor(ContextCompat.getColor(activity, R.color.delayTask));
                    holder.startTime.setTextColor(ContextCompat.getColor(activity, R.color.delayTask));
                    setVisible(1, holder);
                }
                //日程时间小于当前时间
                else {
                    holder.TaskTitle.setTextColor(ContextCompat.getColor(activity, R.color.indateTask));
                    holder.shopName.setTextColor(ContextCompat.getColor(activity, R.color.indateTask));
                    holder.startTime.setTextColor(ContextCompat.getColor(activity, R.color.indateTask));
                    setVisible(1, holder);
                }
                holder.scheduleDetail.setVisibility(View.VISIBLE);
                break;
            case 2:
                holder.TaskTitle.setTextColor(ContextCompat.getColor(activity, R.color.indateTask));
                holder.shopName.setTextColor(ContextCompat.getColor(activity, R.color.indateTask));
                holder.startTime.setTextColor(ContextCompat.getColor(activity, R.color.indateTask));
                setVisible(1, holder);
                holder.scheduleDetail.setVisibility(View.VISIBLE);
                break;
            case 3:
                if (bean.getIsNeedShopReport() == 1 || bean.getIsNeedShopReport() == 2) {
                    holder.TaskTitle.setTextColor(ContextCompat.getColor(activity, R.color.completeEva));
                    holder.shopName.setTextColor(ContextCompat.getColor(activity, R.color.completeEva));
                    holder.startTime.setTextColor(ContextCompat.getColor(activity, R.color.completeEva));
                    setVisible(2, holder);
                } else {
                    setVisible(2, holder);
                }
                holder.scheduleDetail.setVisibility(View.GONE);
                break;
            case 4:
                if (bean.getIsNeedShopReport() == 0 || bean.getIsNeedShopReport() == 4) {
                    holder.TaskTitle.setTextColor(ContextCompat.getColor(activity, R.color.allComplete));
                    holder.shopName.setTextColor(ContextCompat.getColor(activity, R.color.allComplete));
                    holder.startTime.setTextColor(ContextCompat.getColor(activity, R.color.allComplete));
                    setVisible(2, holder);
                } else {
                    setVisible(2, holder);
                }
                holder.scheduleDetail.setVisibility(View.GONE);
                break;
            default:
                //其他情况
                if(holder.TaskTitle.getText().equals("休息")||
                        holder.TaskTitle.getText().equals("年假")||
                        holder.TaskTitle.getText().equals("其他假")||
                        bean.getShopID()==0
                        )
                {
                    holder.submitInfo.setVisibility(View.GONE);
                    holder.shopSign.setVisibility(View.GONE);
                    holder.addinfo.setVisibility(View.GONE);
                    holder.lookInfo.setVisibility(View.GONE);
                    holder.scheduleDetail.setVisibility(View.GONE);
                    holder.startTime.setText(bean.getSchStartTime().split("T")[0]);
                }
                else{
                    holder.TaskTitle.setText(holder.TaskTitle.getText()+"(日程未提交，请到日程校验中提交)");
                    holder.submitInfo.setVisibility(View.GONE);
                    holder.shopSign.setVisibility(View.GONE);
                    holder.addinfo.setVisibility(View.GONE);
                    holder.lookInfo.setVisibility(View.GONE);
                    holder.scheduleDetail.setVisibility(View.VISIBLE);
                }

                break;
        }
        //休息的处理
        if(holder.TaskTitle.getText().equals("休息")
                ||holder.TaskTitle.getText().equals("年假")
                || holder.TaskTitle.getText().equals("其他假")
                ||bean.getShopID()==0)
        {
            holder.submitInfo.setVisibility(View.GONE);
            holder.shopSign.setVisibility(View.GONE);
            holder.addinfo.setVisibility(View.GONE);
            holder.lookInfo.setVisibility(View.GONE);
            holder.scheduleDetail.setVisibility(View.GONE);
            holder.startTime.setText(bean.getSchStartTime().split("T")[0]);
        }

        if(bean.getShopID()==-1 && bean.getIsAssign()==-1)//新版备注
        {
            holder.submitInfo.setVisibility(View.GONE);
            holder.shopSign.setVisibility(View.GONE);
            holder.addinfo.setVisibility(View.GONE);
            holder.lookInfo.setVisibility(View.GONE);
            holder.scheduleDetail.setVisibility(View.VISIBLE);
            holder.shopName.setVisibility(View.VISIBLE);
            holder.shopName.setText(bean.getSubtitle());
            holder.startTime.setText(bean.getSchStartTime().split("T")[0]);
        }
        else if(bean.getSubtitle()!=null&&bean.getSubtitle().equals("自定义任务"))//新版自定义任务
        {
            //holder.submitInfo.setVisibility(View.VISIBLE);
            //holder.shopSign.setVisibility(View.GONE);
            //holder.addinfo.setVisibility(View.VISIBLE);
            //holder.lookInfo.setVisibility(View.VISIBLE);
            holder.scheduleDetail.setVisibility(View.VISIBLE);
            holder.shopName.setVisibility(View.VISIBLE);
            holder.shopName.setText(bean.getSubtitle());
            holder.startTime.setText(bean.getSchStartTime().replace("T"," "));
        }



        /*保存点击的position，以便获取*/
//        holder.itemView.setTag(position);
    }
    private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(activity)
                .setMessage(message)
                .setPositiveButton("确定", okListener)
                .setNegativeButton("取消", null)
                .create()
                .show();
    }

    public void setVisible(int status,MyViewHolder holder ){
        if (status==2){
            holder.submitInfo.setVisibility(View.GONE);
            holder.shopSign.setVisibility(View.GONE);
            holder.addinfo.setVisibility(View.GONE);
            holder.lookInfo.setVisibility(View.VISIBLE);
        }else{
            holder.lookInfo.setVisibility(View.GONE);
            holder.submitInfo.setVisibility(View.VISIBLE);
            holder.shopSign.setVisibility(View.VISIBLE);
            holder.addinfo.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }



    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView TaskTitle;
        TextView startTime;
        TextView shopName;
        TextView submitInfo;
        TextView lookInfo;
        TextView shopSign;
        TextView addinfo;
        CardView cardView;
        ImageView scheduleDetail;

        public MyViewHolder(View view){
            super(view);
            TaskTitle= (TextView) view.findViewById(R.id.TaskTitle);
            startTime= (TextView) view.findViewById(R.id.startTime);
            shopName= (TextView) view.findViewById(R.id.shopName);
            lookInfo= (TextView) view.findViewById(R.id.lookInfo);
            submitInfo= (TextView) view.findViewById(R.id.submitInfo);
            shopSign= (TextView) view.findViewById(R.id.shopSign);
            addinfo= (TextView) view.findViewById(R.id.addinfo);
            cardView=(CardView)view.findViewById(R.id.schCV);
            scheduleDetail= (ImageView) view.findViewById(R.id.scheduleDetail);
            lookInfo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.lookClick((Schedule) lookInfo.getTag(), getLayoutPosition());
                }
            });
            submitInfo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.submitClick((Schedule) submitInfo.getTag(), getLayoutPosition());
                }
            });
            shopSign.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.shopSignClick((Schedule) shopSign.getTag(), getLayoutPosition());
                }
            });
            addinfo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.addinfoClick((Schedule) addinfo.getTag(), getLayoutPosition());
                }
            });
            scheduleDetail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.goDetailClick((Schedule) scheduleDetail.getTag(),getLayoutPosition());
                }
            });
        }
        public void bindTag(Schedule schedule){
            lookInfo.setTag(schedule);
            submitInfo.setTag(schedule);
            shopSign.setTag(schedule);
            addinfo.setTag(schedule);
            scheduleDetail.setTag(schedule);
        }
    }




    /*时间的比较*/
    public static int compare_date(String DATE1, String DATE2) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }


}
